//
//  vdCheckbox.m
//  Void Dead
//
//  Created by Sidney Just on 19.12.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdCheckbox.h"


@implementation vdCheckbox
@synthesize checked, enabled;

#pragma mark -
#pragma mark Touches

- (void)updateString {
	if(message)
	{
		message.pos_x = pos_x + size_x + 5;
		message.pos_y = pos_y + ((size_y / 2) - (message.size_y / 2));;
	}
}

- (void)endSequence:(CGPoint)point {
	point = [[vdKernel sharedKernel] convertOpenGLToUI:point];
	
	if(enabled)
	{
		if(((point.x >= aPos_x && point.x <= aPos_x + size_x) && (point.y >= aPos_y && point.y <= aPos_y + size_y)) ||
		   ((point.x >= message.aPos_x && point.x <= message.aPos_x + message.size_x) && (point.y >= message.aPos_y && point.y <= message.aPos_y + message.size_y)))
		{
			if(!checked)
			{
				if(stateChecked)
				{
					[super setTexture:stateChecked];
					checked = YES;
					
					if(delegate)
					{
						if([delegate conformsToProtocol:@protocol(vdInterfaceProtocol)])
						{
							if([delegate respondsToSelector:@selector(checkBoxChangedState:)])
								[delegate checkBoxChangedState:self];
						}
					}
				}
			} else {
				if(stateNormal)
				{
					[super setTexture:stateNormal];
					checked = NO;
					
					if(delegate)
					{
						if([delegate conformsToProtocol:@protocol(vdInterfaceProtocol)])
						{
							if([delegate respondsToSelector:@selector(checkBoxChangedState:)])
								[delegate checkBoxChangedState:self];
						}
					}
				}
			}
		}
	}
	
	[self updateString];
}

#pragma mark -
#pragma mark Textures

- (void)setDelegate:(id)del {
	delegate = del;
}

- (void)setStateNormal:(vdTexNode *)tex {
	stateNormal = tex;
	
	if(!checked)
	{
		[super setTexture:stateNormal];
		[self updateString];
	}
}

- (void)setStateChecked:(vdTexNode *)tex {
	stateChecked = tex;
	
	if(checked)
	{
		[super setTexture:stateChecked];
		[self updateString];
	}
}

- (void)setString:(NSString *)str {
	if(str)
	{
		if(!message)
		{
			message = [vdLabel labelWithText:str];
			[self addChild:message];
		} else {
			[message setString:str];
		}
	} else {
		[self removeChild:message cleanUp:YES];
		[message release];
		message = NULL;
	}
	
	[self updateString];
}

#pragma mark -

- (id)init {
	if(self = [super initWithTexture:NULL])
	{
		stateNormal = NULL;
		stateChecked = NULL;
		
		checked = NO;
		
		enabled = YES;
		
		message = NULL;
		delegate = NULL;
	}
	return self;
}

- (void)dealloc {
	[super dealloc];
}

@end
